#!/bin/bash

# Tiny QA - 模型状态检查脚本 (单容器版本)

echo "========================================"
echo "Tiny QA - 模型状态检查"
echo "========================================"

# 检查Ollama服务是否运行
if ! curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then
    echo "❌ Ollama服务未运行"
    echo "请检查容器状态: docker logs tiny-qa"
    exit 1
fi

echo "✅ Ollama服务正在运行"

# 检查模型下载状态
echo ""
echo "🔍 检查模型下载状态..."

# 获取模型列表
models_output=$(curl -s http://localhost:11434/api/tags 2>/dev/null)

if [ $? -eq 0 ] && [ -n "$models_output" ]; then
    echo "✅ 模型列表获取成功:"
    echo "$models_output" | jq -r '.models[] | "  - \(.name) (\(.size))"' 2>/dev/null || echo "$models_output"
    
    # 检查是否有对话模型
    if echo "$models_output" | grep -q "qwen2.5"; then
        echo ""
        echo "✅ 对话模型已安装"
    else
        echo ""
        echo "⚠️ 对话模型未安装，正在下载..."
        curl -X POST http://localhost:11434/api/pull -d '{"name": "qwen2.5:7b"}'
    fi
    
    # 检查是否有嵌入模型
    if echo "$models_output" | grep -q "nomic-embed-text"; then
        echo "✅ 嵌入模型已安装"
    else
        echo "⚠️ 嵌入模型未安装，正在下载..."
        curl -X POST http://localhost:11434/api/pull -d '{"name": "nomic-embed-text"}'
    fi
    
else
    echo "❌ 无法获取模型列表"
    echo "请检查Ollama服务状态"
fi

# 检查服务健康状态
echo ""
echo "🏥 服务健康检查..."

# 检查Ollama API
if curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then
    echo "✅ Ollama API 正常"
else
    echo "❌ Ollama API 异常"
fi

# 检查Tiny QA API
if curl -s http://localhost:5002/api/health > /dev/null 2>&1; then
    echo "✅ Tiny QA API 正常"
else
    echo "❌ Tiny QA API 异常"
fi

echo ""
echo "========================================"
echo "检查完成！"
echo "========================================"
echo ""
echo "📋 常用命令:"
echo "查看模型: curl http://localhost:11434/api/tags"
echo "下载模型: curl -X POST http://localhost:11434/api/pull -d '{\"name\": \"<model_name>\"}'"
echo "查看日志: docker logs tiny-qa"
echo "进入容器: docker exec -it tiny-qa bash" 